<template>
  <div class="app-container">
    <!-- 条件栏 -->
    <div class="filter-wrapper">
      <NewPageFilter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
        :filter-btn="filterInfo.btn"
      >
        <template v-slot:filter-operateTime>
          <el-date-picker
            v-model="operateTime"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="至"
            :clearable="false"
            :picker-options="$fn.pickerOptionsFn()"
            align="right"
            start-placeholder="操作时间开始日期"
            end-placeholder="操作时间结束日期"
            :shortcuts="$fn.pickerOptionsFn()"
            :default-time="[
              new Date(2000, 1, 1, 0, 0, 0),
              new Date(2000, 1, 1, 23, 59, 59),
            ]"
            :required="true"
            @change="timeChange"
          ></el-date-picker>
        </template>
      </NewPageFilter>
    </div>
    <div class="filter-wrapper">
      <NewPageFilter :filter-btn="filterInfo.btnList"></NewPageFilter>
    </div>
    <!-- 表格 -->
    <TableContainer
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      :pageTotal="pageInfos.pageTotal"
      :api="api"
      v-model:data="tableInfo.data"
      v-model:query="filterInfo.query"
      :tab-index="false"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :pager="true"
      :checkBox="false"
    />
  </div>
</template>

<script setup name="pageDeptSatisfaction">
import {
  getDeptSatisfaction,
  excelDeptSatisfaction,
  printDeptSatisfaction,
} from '@/api/dataQuery'
import moment from 'moment'
import { fatchTemplateByCode } from '@/api/base'
const { proxy } = getCurrentInstance()

const operateTime = ref([])
const pageInfos = ref({
  pageSize: 20,
  curPage: 1,
  pageTotal: 0,
})
const api = ref(null)
// 过滤相关配置
const filterInfo = ref({
  query: {
    completeTime_from: '',
    completeTime_to: '',
  },
  copyQuery: {},
  list: [
    {
      slot: true,
      label: '起止日期',
      value: 'operateTime',
    },
  ],
  btn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      type: 'button',
      label: '导出excel',
      event: exportExcel,
      show: proxy.$permission('pageDeptSatisfaction:excel'),
    },
    {
      type: 'button',
      label: '打印',
      event: print,
      show: proxy.$permission('pageDeptSatisfaction:print'),
    },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: '部门', value: 'maintenanceDeptName' },
    { label: '报修总量', value: 'repairOrderQtyTotal' },
    { label: '满意', value: 'satisfactionOrderQty' },
    { label: '不满意', value: 'unSatisfactionOrderQty' },
    { label: '满意度', value: 'satisfactionRate' },
  ],
})

onMounted(() => {
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
})

function exportExcelList() {
  let params = {
    ...filterInfo.value.query,
    curPage: pageInfos.value.curPage,
    pageSize: pageInfos.value.pageSize,
  }
  excelDeptSatisfaction(params).then((res) => {
    if (res) {
      proxy.$fn.parseExcel(res)
    }
  })
}
// 获取列表
function getList() {
  api.value = getDeptSatisfaction
  tableInfo.value.refresh = Math.random()
}
function clearSearch() {
  operateTime.value = []
  filterInfo.value.query.completeTime_from = ''
  filterInfo.value.query.completeTime_to = ''
  getList()
}
//打印
function print() {
  fatchTemplateByCode({
    resourceCode: 'pageDeptSatisfaction:print',
    isDefault: 1,
  }).then((res) => {
    if (res && res.success) {
      let temp = proxy.$fn.getPrintTemp(res.data[0])
      getPrintData(temp)
    }
  })
}
function getPrintData(temp) {
  let queryParams = {
    curPage: pageInfos.value.curPage,
    pageSize: pageInfos.value.pageSize,
    ...filterInfo.value.query,
  }
  printDeptSatisfaction({ queryParams: queryParams }).then((res) => {
    if (res.success && res) {
      let data = proxy.$fn.deepClone(res.data)
      let newData = data.map((item) => {
        return {
          ...item,
          ...item.orderInfo,
        }
      })
      proxy.$lodop.preview(temp, newData)
    }
  })
}
// 搜索
function search() {
  getList()
}
function timeChange(val) {
  if (val && val.length) {
    filterInfo.value.query.completeTime_from = val[0]
    filterInfo.value.query.completeTime_to = val[1]
  } else {
    filterInfo.value.query.completeTime_from = ''
    filterInfo.value.query.completeTime_to = ''
  }
  search()
}
function exportExcel() {
  exportExcelList()
}
</script>

<style scoped></style>
